package com.lionmibi.mynotification;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Binder;
import android.os.IBinder;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.support.annotation.IntDef;
import android.util.Log;

/**
 * Created by LionMibi on 2018/3/3.
 */

public class NotificationListener extends NotificationListenerService {
    //有新通知
    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        super.onNotificationPosted(sbn);
        Log.d("Notification","有新通知");
    }

    //有通知移除
    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {
        super.onNotificationRemoved(sbn);
        Log.d("Notification","有通知移除");
    }

    private MyBroadcastReceiver myBroadcastReceiver;
    //Listener可用且连接到通知管理器
    @Override
    public void onListenerConnected() {
        super.onListenerConnected();
        Log.d("Notification","Listener可用且连接到通知管理器");

        myBroadcastReceiver=new MyBroadcastReceiver();
        IntentFilter intentFilter=new IntentFilter();
        intentFilter.addAction("AAAA");
        registerReceiver(myBroadcastReceiver,intentFilter);
        Log.d("Notification","Broadcast绑定");

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_NOT_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(myBroadcastReceiver);
        Log.d("Notification","Service被销毁，广播解除绑定");
    }

    public void delete(){
        cancelAllNotifications();
    }

    public class MyBroadcastReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            delete();
        }
    }


}
